﻿/************************************************************************************
 * xujiajun@yoozoo.com 御剑
 * 层级容器管理器
 ***********************************************************************************/

using System.Collections.Generic;

namespace WorldMapBase.Layer
{
    /// <summary>
    /// 层级容器管理器
    /// </summary>
    public class LayerContainerManager
    {
        /// <summary>
        /// 所有容器列表
        /// </summary>
        private List<LayerContainer> m_LayerContainerList = new List<LayerContainer>();

        /// <summary>
        /// 所有容器的数量
        /// </summary>
        private int m_LayerContainerCnt;

        /// <summary>
        /// 用于生成容器ID的计数
        /// </summary>
        private int m_LayerContainerIndex = 0;

        /// <summary>
        /// 往管理器中添加容器，添加完成后会返回容器ID，ID是从1开始的正整数，添加失败会返回-1
        /// </summary>
        /// <param name="container">容器对象</param>
        /// <returns>容器ID</returns>
        public int AddLayerContainer(LayerContainer container,int layerId = -1)
        {
            if (m_LayerContainerList != null)
            {
                m_LayerContainerList.Add(container);
                m_LayerContainerIndex++;
                m_LayerContainerCnt++;
                container.id = m_LayerContainerIndex;
                container.layerId = layerId;
                return m_LayerContainerIndex;
            }
            return -1;
        }

        /// <summary>
        /// 从管理器中移除指定容器
        /// </summary>
        /// <param name="container">容器对象</param>
        public void RemoveContainer(LayerContainer container)
        {
            RemoveContainer(container.id);
        }

        /// <summary>
        /// 从管理器中移除指定ID的容器
        /// </summary>
        /// <param name="containerId">容器ID</param>
        public void RemoveContainer(int containerId)
        {
            for (int i = 0; i < m_LayerContainerCnt; i++)
            {
                var container = m_LayerContainerList[i];
                if (containerId == container.id)
                {
                    container.Clear();
                    m_LayerContainerList.RemoveAt(i);
                    m_LayerContainerCnt--;
                    break;
                }
            }
        }

        /// <summary>
        /// 从管理器中获取指定ID的容器
        /// </summary>
        /// <param name="containerId">容器ID</param>
        /// <returns>容器对象</returns>
        public LayerContainer GetContainer(int containerId)
        {
            for (int i = 0; i < m_LayerContainerCnt; i++)
            {
                var container = m_LayerContainerList[i];
                if (containerId == container.id)
                {
                    return container;
                }
            }
            return null;
        }

        /// <summary>
        /// 从管理器中获取指定Layer的容器
        /// </summary>
        /// <param name="layer">容器LayerID</param>
        /// <returns>容器对象</returns>
        public LayerContainer GetLodContainer(int layerID)
        {
            for (int i = 0; i < m_LayerContainerCnt; i++)
            {
                var container = m_LayerContainerList[i];
                if (layerID == container.layerId)
                {
                    return container;
                }
            }
            return null;
        }

        /// <summary>
        /// 轮询更新
        /// </summary>
        public void Update()
        {
            for (int i = 0; i < m_LayerContainerCnt; i++)
            {
                var container = m_LayerContainerList[i];
                container.Update();
            }
        }

        /// <summary>
        /// 清空所有容器内的数据
        /// </summary>
        public void Clear()
        {
            for (int i = 0; i < m_LayerContainerCnt; i++)
            {
                var container = m_LayerContainerList[i];
                container.Clear();
            }
            m_LayerContainerCnt = 0;
            m_LayerContainerList.Clear();
        }
    }

}
